﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
///  模組: CommonUtil Module
///  作者: Adams <adams@netdb.com.tw>
///  修改: 2013-2-14
///  目的: 一般性常用功能
///  參數: 無
/// </summary>
public class CommonUtil
{
    public static void DisplayErrorMessage(string message, Label lbl)
    {
        lbl.EnableViewState = false;
        lbl.ForeColor = System.Drawing.Color.Red;
        lbl.Text = message;
    }

    public static void DisplayInfoMessage(string message, Label lbl)
    {
        lbl.EnableViewState = false;
        lbl.ForeColor = System.Drawing.Color.Blue;
        lbl.Text = message;
    }

    public static string CheckCurrentyyyyMMdd(string yyyyMMdd)
    {
        string dateString = yyyyMMdd;
        if (!string.IsNullOrEmpty(dateString))
        {
            int result = 0;
            if (dateString.Length == 8 && int.TryParse(dateString, out result))
            {

                dateString = dateString.Substring(0, 4) + "/" +
                              dateString.Substring(4, 2) + "/" +
                              dateString.Substring(6, 2);
                DateTime date;
                if (!DateTime.TryParse(dateString, out date))
                {
                    yyyyMMdd = "99999999";
                }
            }
        }
        return yyyyMMdd;
    }

    public static string TransferyyyyMMddToDate(string yyyyMMdd)
    {
        string dateString = yyyyMMdd;
        if (!string.IsNullOrEmpty(dateString))
        {
            int result = 0;
            if (dateString.Length == 8 && int.TryParse(dateString, out result))
            {
                dateString = dateString.Substring(0, 4) + "/" +
                              dateString.Substring(4, 2) + "/" +
                              dateString.Substring(6, 2);
                DateTime date;
                if (!DateTime.TryParse(dateString, out date))
                {
                    yyyyMMdd = "99999999";
                }
                else
                {
                    yyyyMMdd = dateString;
                }
            }
        }
        return yyyyMMdd;
    }

    public static string TransferhhmmssToTime(string hhmmss)
    {
        string TimeString = hhmmss;
        if (!string.IsNullOrEmpty(TimeString))
        {
            int result = 0;
            if (TimeString.Length == 6 && int.TryParse(TimeString, out result))
            {
                TimeString = TimeString.Substring(0, 2) + ":" +
                              TimeString.Substring(2, 2) + ":" +
                              TimeString.Substring(4, 2);
                hhmmss = TimeString;
            }
        }
        return TimeString;
    }
}